package com.xyzh05.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.xyzh05.dao.SchoolDao;
import com.xyzh05.model.School;

@Repository("schoolDao")
public class SchoolDaoHibernate extends GenericDaoHibernate<School, Long> implements SchoolDao {

    public SchoolDaoHibernate() {
        super(School.class);
    }

	@SuppressWarnings("unchecked")
	public List<School> querySchoolAutoComplete(String keyword) {
		List<Object> plist = new ArrayList<Object>();
    	StringBuilder hql = new StringBuilder();
    	
    	hql.append("select new School(s.id, s.name, s.country) from School s where 1 = 1");
    	if(keyword != null && !"".equals(keyword)){
    		hql.append(" and s.name like ?");
    		plist.add("%" + keyword + "%");
    	}
    	hql.append(" order by s.name");
    	
    	List<School> list = this.getHibernateTemplate().find(hql.toString(), plist.toArray());   
    	return list;
	}
}
